package org.myOrg.spetrum.Listener;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.myOrg.spetrum.util.Message;
import org.myOrg.spetrum.util.MessageImplement;
import org.myOrg.spetrum.util.MessageMangement;
import org.myOrg.spetrum.util.MessageMangementImplement;
import org.myOrg.spetrum.util.OverallFactory;
import org.myOrg.spetrum.util.Sender;

public class MenuBarListener implements ActionListener, Sender {
	private MessageMangement mangerment;

	public MenuBarListener() {
		this.setMessageMangement(OverallFactory.getMessageManger());
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if (e.getActionCommand().equals("select Suite")) {
			JFileChooser fc = new JFileChooser(".");
			fc.setDialogTitle("Open File"); 
			int returnVal = fc.showOpenDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();

				Message msg = new MessageImplement(this,
						MessageMangementImplement.GETINPUTPATH, file.getPath());
				this.sendMessage(msg);
			}
		} else if (e.getActionCommand().equals("about")) {
			String msg = "Vision:1.0\r\nAuthor: Xintao Niu";
			JOptionPane.showMessageDialog((Component) e.getSource(), msg,
					"About Specttum Analyse Software",
					JOptionPane.INFORMATION_MESSAGE);
		}else if(e.getActionCommand().equals("save result")){
			JFileChooser fc = new JFileChooser(".");
			fc.setDialogTitle("Save File"); 
			int returnVal = fc.showSaveDialog(null);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File file = fc.getSelectedFile();

				Message msg = new MessageImplement(this,
						MessageMangementImplement.GETOUTPUTPATH, file.getPath());
				this.sendMessage(msg);
			}
		}
	}

	@Override
	public void sendMessage(Message msg) {
		// TODO Auto-generated method stub
		mangerment.dispatchMeesage(msg);
	}

	@Override
	public void setMessageMangement(MessageMangement mangerment) {
		// TODO Auto-generated method stub
		this.mangerment = mangerment;
	}

}
